class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int n = nums.size();
        int ans = -10;
        vector<int> f(n + 1, 0);
        vector<int> g(n + 1, 0);
        f[0] = g[0] = 1;
        for (int i = 1; i <= n; i++) {
            f[i] = max(nums[i - 1],
                       max(f[i - 1] * nums[i - 1], g[i - 1] * nums[i - 1]));
            g[i] = min(nums[i - 1],
                       min(f[i - 1] * nums[i - 1], g[i - 1] * nums[i - 1]));
            ans = max(ans, f[i]);
        }
        return ans;
    }
};